/*
 * Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
 * trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with trinea.cn.
 */
package com.twotiger.and.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.bean.Notice;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.URLUtil;

/**
 * NoticeAdapter
 *
 */
public class NoticeAdapter extends PagerAdapter {

    private BaseActivity context;
    private List<Notice> noticeList;

    public NoticeAdapter(Context context, List<Notice> noticeList) {
        this.context = (BaseActivity) context;
        this.noticeList = noticeList;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    public Notice getCurrentNotice(int item) {
        int index = ListUtils.isEmpty(noticeList) ? 0 : item % noticeList.size();
        Notice mNotice = noticeList.get(index);
        return mNotice;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        View view = View.inflate(context, R.layout.notice_viewpager_item, null);
        TextView textview = (TextView) view.findViewById(R.id.tv_boutique_notice);

        int index = ListUtils.isEmpty(noticeList) ? 0 : position % noticeList.size();
        // ((ViewPager)container).addView(iv);
        if (!ListUtils.isEmpty(noticeList)) {
            if (((ViewPager) container).getChildCount() == noticeList.size()) {
                ((ViewPager) container).removeView(view);
            }
        }

        ((ViewPager) container).addView(view);
        if (ListUtils.isEmpty(noticeList)) {
            return view;
        }
        final Notice mNotice = noticeList.get(index);
        textview.setText(mNotice.getTitle());
        textview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(mNotice.getId())) {
                    return;
                }
                Intent intent = new Intent(context, WebViewPage.class);
                intent.putExtra("title", mNotice.getTitle());
                Map<String, String> contractmap = new HashMap<String, String>();
                contractmap.put("id", mNotice.getId());
                intent.putExtra("url",
                    Constants.NOTICEINFOHTML + "?" + URLUtil.createContractUrl(contractmap, "3.0"));

                context.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            }
        });
        return view;
    }

    @Override
    public int getCount() {
        return ListUtils.isEmpty(noticeList) ? 0 : noticeList.size() * 300;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

}
